<template>
    <div class="bigsearchbox">
        <!-- 搜索页 -->
        <div v-show="!xs">
            <div class="searchtop">
                <div class="sshuru">
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-shouye5"></use>
                    </svg>
                    <input type="text" placeholder="搜索单曲" v-model="sousuo" autofocus ref="input" v-focus id="shu">
                    <span class="qc" @click="qinchu">×</span>
                </div>
                <div @click="fanhui()" class="qx">
                    取消
                </div>
            </div>
            <div class="searchdown">
                <ul class="searchend" v-show="cha">
                    <li v-for="item in searcharr" :key="item.id">
                        <div @click="go(item)">
                            <p class="searchsong" @click="addhis(item)">
                                <svg class="icon" aria-hidden="true">
                                    <use xlink:href="#icon-shouye5"></use>
                                </svg>
                                <span>{{item.name}}</span>
                                ——
                                <span>{{item.people}}</span> 
                            </p>
                        </div>
                    </li>
                    <li v-show="unde" class="nofind">啥都找不到哦</li>
                    <li class="finding">
                        <div class="loading">
                            <loading v-show="ssz"></loading>
                        </div>
                    </li>
                </ul>
                <div v-show="!cha">
                    <h4 class="searchtou">热门搜索</h4>
                    <ul class="hotsearch">
                        <li v-for="item in hot" :key="item.id" @click="addhis(item)">
                            <div @click="ss(item)">
                                {{item.name}}
                            </div>
                        </li>
                    </ul>
                    <h4 class="searchtou">
                        历史记录
                        <span @click="quanshan">
                            <svg class="icon" aria-hidden="true">
                                <use xlink:href="#icon-icon4"></use>
                            </svg>
                        </span> 
                    </h4>
                    <ul class="hissearch">
                        <li v-for="(item,index) in his" :key="item.id">
                            <div @click="ss(item)">
                                <span @click="quchong(2,item)">{{item.name}}</span>
                            </div>
                            <span @click="shanyige(index)">×</span>
                        </li>
                    </ul>
                </div>
            </div>            
        </div>
        <loading v-show="xs" class="top"></loading>
    </div>
</template>
<script>
import loading from "../small/loading.vue"
export default {
    name:"search",
    data(){
        return {
            sousuo:'',
            hot:[],
            his:[],
            xs:true,
            ssz:false,
            cha:false,
            tim:null,
            unde:false,
            searcharr:[],
        }
    },
    components:{
        loading
    },
    created(){
        if(sessionStorage.getItem("searchhis")!=null){
            if(sessionStorage.getItem("searchhis") != '' || sessionStorage.getItem("searchhis") != "[]"){
                this.his = JSON.parse(sessionStorage.getItem("searchhis"))
            }else{
                this.his = []
            }
        }
        this.getdata()
    },
    watch:{
        'sousuo'(){
            if(this.sousuo == ''){
                this.cha = false
            }else{
                this.ssz = true
                this.cha = true
                this.searcharr = []
                this.jieliu()
            }
            this.unde = false
        },
        "his"(){
            if(typeof this.his == "string"){
                this.his = []
            }
        }
    },
    methods:{
        addhis(item){
            this.quchong(1,item)
        },
        quanshan(){
            this.his= JSON.stringify([])
            sessionStorage.setItem("searchhis",JSON.stringify(this.his))
        },
        shanyige(index){
            let newarr=[]
            for(let i = 0;i<this.his.length;i++){
                if(i === index){}else{
                    newarr.push(this.his[i])
                }
            }
            this.his = newarr
            sessionStorage.setItem("searchhis",JSON.stringify(this.his))
        },
        qinchu(){
            this.sousuo = ''
        },
        fanhui(){
            this.$router.go(-1)
        },
        getdata(){
            axios.get(`https://apis.netstart.cn/music/personalized/newsong`,
            {
                params: {}
            }
            ).then(
                res=>{
                    // console.log(res)
                    let arr = res.data.result.slice(0,8)
                    let newarr = []
                    for(let i = 0;i<arr.length;i++){
                        let obj = {}
                        obj.name=arr[i].name
                        obj.id = arr[i].id
                        obj.people = ''
                        for(let j = 0;j<arr[i].song.artists.length;j++){
                            obj.people += arr[i].song.artists[j].name + ' '
                        }
                        newarr.push(obj)
                    }
                    this.hot = newarr
                    this.xs = false
                    // document.getElementById('shu').focus()
                    // this.$refs.input.focus();
                },
                err=>{
                    console.log(err)
                }
            )
        },
        getsearch(){
            let that = this
            axios.get(`https://apis.netstart.cn/music/cloudsearch?keywords=${this.sousuo.replace(' ',',')}`,
            {
                params: {

                }
            }
            ).then(
                res=>{
                    // console.log(res)
                    if(res.data.result.songCount == 0){
                        this.ssz = false
                        this.unde = true
                    }
                    if(res.data.result.songs!==undefined){
                        let arr = res.data.result.songs
                        let newarr = []
                        for(let i = 0;i<arr.length;i++){
                            let obj = {}
                            obj.id = arr[i].id
                            obj.name = arr[i].name
                            obj.people = ''
                            for(let j = 0;j<arr[i].ar.length;j++){
                                if(j == 0){
                                    obj.people += arr[i].ar[j].name + ' '
                                }else{
                                    obj.people += "、" + arr[i].ar[j].name 
                                }
                            }
                            newarr.push(obj)
                        }
                        this.searcharr = newarr
                        this.ssz = false
                        this.unde = false
                    }
                },
                err=>{
                    console.log(err)
                }
            )
        },
        jieliu(){
            clearTimeout(this.tim)
            this.ssz = true
            this.tim = null
            this.tim = setTimeout(()=>{
                // console.log(this.sousuo)
                if(this.sousuo !== ''){
                    this.getsearch()
                }
                clearTimeout(this.tim)
                this.tim = null
                // this.unde = false
            },2000)
        },
        quchong(num,item){
            if(num == 1){
                for(let i = 0;i<this.his.length;i++){
                    if(item.id === this.his[i].id){
                        this.shanyige(i)
                    }
                }
                this.his.unshift(item)
                if(this.his.length >= 8){
                    this.his = this.his.slice(0,10)
                }
            }else{
                for(let i = 0;i<this.his.length;i++){
                    if(item.id === this.his[i].id){
                        this.shanyige(i)
                    }
                }
                this.his.unshift(item)
                // console.log(item)
            }
            let arr = JSON.stringify(this.his)
            sessionStorage.setItem("searchhis",arr)
        },
        go(value){
            this.$store.commit("changesongid",value.id)
            this.$store.commit("changesongstate",true)
            this.$router.push({path:'/songdetails'})
            // console.log(value.id)
        },
        ss(value){
            this.sousuo = value.name
        }
    }
}
</script>
<style lang="less" scoped>
a{ 
    text-decoration: none;
    color:#a3a3a3 ;
}
ul,li{
    margin: 0;
    padding: 0;
    list-style: none;
}
.bigsearchbox{
    height: 100%;
    .searchtop{
        padding: 0px 8%;
        display: flex;
        // justify-content: space-between;
        .qx{
            margin-left: 5%;
            margin-top: 10%;
            color: #646464;
            font-size: 0.9rem;
            font-family: 'YouYuan';
            font-weight: 600;
        }
    }
    .searchdown{}
}
.sshuru{
    position: relative;
    margin-top:8.5%;
    width: 80%;
    // margin-left: 5%;
    svg{
        position: absolute;
        top:0px;
        left:0px;
        color: #a3a3a3;
        font-size: 1.3rem;
        top: 10%;
        left: 2%;
    }
    input{
        border: 1px solid #a3a3a3;
        outline: none;
        width: 100%;
        height: 25px;
        padding-left:30px;
        box-sizing: border-box;
        border-radius: 15px;
        padding-right:30px;
        // background: rgba(255, 255, 255, 0.3);
        &::-webkit-input-placeholder{
            // color:rgba(255, 255, 255, 0.698);
            color: #a3a3a3;
        }
    }
    span{
        display: inline-block;
        position: absolute;
        top: 7%;
        right: 2%;
        width: 20px;
        line-height: 20px;
        font-size: 25px;
        height: 20px;
        border-radius: 50%;
        // background-color: #eee;
        color: #a3a3a3;
        // text-shadow: 0px 0px 3px #bbb;
        text-align: center;
    }
}
.searchdown{
    padding: 0px 10%;
    position: relative;
}
.searchend{
    // position: absolute;
    background-color: white;
    padding-bottom: 30px;
    // top: -10%;
    // left: 10%;
    // width: 80%;
    li{
        width: 80%;
        padding-bottom: 0px;
        border-bottom: 1px solid #ccc;
    }    
}
.searchsong{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    svg{
        margin-right: 5px;
    }
    span{

    }
}
.searchtou{
    font-family: 'YouYuan';
    color: #646464;
    display: flex;
    justify-content: space-between;
    span{

        svg{
            margin-right: 20px;
            font-size: 1.2rem;
            color: #646464;
        }
    }
}
.hotsearch{
    display:flex;
    flex-wrap: wrap;
    li{
        background-color:  #f4f4f4;
        border-radius: 50px;
        padding: 5px;
        color: #a3a3a3;
        font-size: 0.8rem;
        margin-right: 10px;
        margin-bottom: 10px;
        font-family: "宋体";
        &:first-child{
            background-color:  #64a6ff54;
            font-weight: 600;
            div{
                color:#4475b7 !important;
            }
        }
        &:nth-child(2){
            background-color:  #64a6ff54;
            font-weight: 600;
            div{
                color:#4475b7 !important;
            }
        }
    }
    
}
.hissearch{
    margin-top: 10px;
    margin-right:25px;
    li{
        display: flex;
        justify-content: space-between;
        font-size: 0.9rem;
        margin-bottom: 10px;
        vertical-align: middle;
        span{
            color: #a3a3a3;
            line-height: 1.5rem;
            &:nth-child(2){
                font-size: 1.5rem;
            }
        }
    }
}
.nofind{
    font-size: 2rem;
    color: #a3a3a3;
    margin-top: 20px;
    text-align: center;
    border:0px solid transparent !important;
}
.top{
    height: 100%;
    width:100%;
}
.finding{
    width: 350px !important;
    height:200px;
    border:0px solid transparent !important;
    font-size: 13px;
    position: relative;
    .loading{
        width: 100%;
        height: 100%;
        position: absolute;
        top:0px;
        left:-10%;
    }
}
</style>